package com.liuboya.service.task.wbhotsearchrank.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.liuboya.common.OkhttpUtils;
import com.liuboya.dao.weibo.wbhotsearchrank.entity.WbHotSearchRank;
import com.liuboya.service.tablebiz.wbhotsearchrank.WbHotSearchRankService;
import com.liuboya.service.task.wbhotsearchrank.WbHotSearchRankTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Objects;

/**
 * 描述：
 *
 * @author yeyuan
 * @date 2021/08/13 15:47
 **/
@EnableScheduling
@Service
@Slf4j
public class WbHotSearchRankTaskImpl implements WbHotSearchRankTask {

    private static final String WEIBO_HOT_RANK_API_URL = "https://api.weibo.cn/2/guest/page?" +
            "&containerid=106003type%253D25%2526t%253D3%2526disable_hot%253D1%2526filter_type%253Drealtimehot";

    @Value("${dev.flag}")
    protected Boolean devFlag;

    @Resource
    private WbHotSearchRankService wbHotSearchRankService;

    //    @Scheduled(cron = "0 5 * * * ?")
    @Scheduled(fixedDelay = 1000 * 60 * 10)
    @Override
    public void wbHotSearchRankEveryHour() {
        log.info("微博热搜榜,开始爬取");
        if (devFlag) {
            return;
        }
        log.info("微博热搜榜,开始爬取");
        String s = OkhttpUtils.commonGet(WEIBO_HOT_RANK_API_URL, new ArrayList<>(), null);
        if (StringUtils.isBlank(s)) {
            log.error("微博热搜榜,无数据");
        }
        JSONObject jsonObject = JSONObject.parseObject(s);
        log.info("微博热搜榜,数据:{}", jsonObject);
        JSONArray cards = jsonObject.getJSONArray("cards");
        for (Object card : cards) {
            JSONObject data = (JSONObject) card;
            String title = data.getString("title");
            String itemid = data.getString("itemid");
            if ((StringUtils.isNotBlank(title) && title.contains("实时热点")) || "hotword".equals(itemid)) {
                handleWbHotSearchRankJson(data);
                break;
            }
        }
        log.info("微博热搜榜,结束");
    }

    public void handleWbHotSearchRankJson(JSONObject data) {
        if (Objects.isNull(data)) {
            return;
        }
        JSONArray cardGroup = data.getJSONArray("card_group");
        if (Objects.isNull(cardGroup) || cardGroup.isEmpty()) {
            return;
        }
        for (Object o : cardGroup) {
            JSONObject item = (JSONObject) o;
            WbHotSearchRank toWbInsert = wbHotSearchRankService.jsonToWbInsert(item);
            if (Objects.isNull(toWbInsert)) {
                continue;
            }
            try {
                wbHotSearchRankService.insert(toWbInsert);
            } catch (Exception e) {
                log.error("微博热搜榜,插入出错:{}", e.getMessage(), e);
            }
        }
    }

}